package cn.com.blueInfo.bpm.person.service.impl;

import cn.com.blueInfo.bpm.person.convert.UserIdentityConvert;
import cn.com.blueInfo.bpm.person.entity.UserIdentity;
import cn.com.blueInfo.bpm.person.repository.UserIdentityRepository;
import cn.com.blueInfo.bpm.person.request.UserIdentityDTO;
import cn.com.blueInfo.bpm.person.response.UserIdentityVO;
import cn.com.blueInfo.bpm.person.service.UserIdentityService;
import cn.com.blueInfo.core.util.SnowflakeIdGenerator;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 员工身份信息表 业务服务实现类
 * @author suxch
 * @since 2025-09-01 19:28:09
 */
@Log4j2
@Service
public class UserIdentityServiceImpl implements UserIdentityService {

    @Autowired
    private UserIdentityRepository userIdentityRepository;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @Autowired
    private UserIdentityConvert userIdentityConvert;

    /**
     * 新增员工身份信息
     * @param userIdentityDTO 员工身份信息参数
     */
    @Override
    public ResultInfo<Integer> addUserIdentity(UserIdentityDTO userIdentityDTO) {
        UserIdentity userIdentity = userIdentityConvert.dtoToEntity(userIdentityDTO);
        userIdentity.setId(snowflakeIdGenerator.nextId());
        Integer addSize = userIdentityRepository.createUserIdentity(userIdentity);
        return ResultInfo.<Integer>builder().data(addSize).message("员工身份信息成功新增 {} 条").build();
    }

    /**
     * 批量新增员工身份信息
     * @param userIdentityDTOList 员工身份信息参数列表
     */
    @Override
    public ResultInfo<Integer> batchAddUserIdentity(List<UserIdentityDTO> userIdentityDTOList) {
        List<UserIdentity> userIdentityList = userIdentityConvert.dtoToEntity(userIdentityDTOList);
        for (UserIdentity userIdentity : userIdentityList) userIdentity.setId(snowflakeIdGenerator.nextId());
        Integer addSize = userIdentityRepository.batchCreateUserIdentity(userIdentityList);
        return ResultInfo.<Integer>builder().data(addSize).message("员工身份信息成功新增 {} 条").build();
    }

    /**
     * 删除员工身份信息
     * @param userIdentityId 员工身份信息ID
     */
    @Override
    public ResultInfo<Integer> removeUserIdentity(Long userIdentityId) {
        Integer removeSize = userIdentityRepository.deleteUserIdentity(userIdentityId);
        return ResultInfo.<Integer>builder().data(removeSize).message("员工身份信息成功删除 {} 条").build();
    }

    /**
     * 批量删除员工身份信息
     * @param userIdentityIdList 员工身份信息ID列表
     */
    @Override
    public ResultInfo<Integer> batchRemoveUserIdentity(List<Long> userIdentityIdList) {
        Integer removeSize = userIdentityRepository.batchDeleteUserIdentity(userIdentityIdList);
        return ResultInfo.<Integer>builder().data(removeSize).message("员工身份信息成功删除 {} 条").build();
    }

    /**
     * 更新部分员工身份信息
     * @param userIdentityUpdatePartialDTO 员工身份信息参数
     */
    @Override
    public ResultInfo<Integer> modifyUserIdentityPartial(
            UserIdentityDTO.UpdatePartialDTO userIdentityUpdatePartialDTO) {
        UserIdentity userIdentity = userIdentityConvert.dtoToEntity(userIdentityUpdatePartialDTO);
        Integer modifySize = userIdentityRepository.updateUserIdentityPartial(userIdentity);
        return ResultInfo.<Integer>builder().data(modifySize).message("员工身份信息成功更新 {} 条").build();
    }

    /**
     * 更新全部员工身份信息
     * @param userIdentityDTO 员工身份信息参数
     */
    @Override
    public ResultInfo<Integer> modifyUserIdentity(UserIdentityDTO userIdentityDTO) {
        UserIdentity userIdentity = userIdentityConvert.dtoToEntity(userIdentityDTO);
        Integer modifySize = userIdentityRepository.updateUserIdentity(userIdentity);
        return ResultInfo.<Integer>builder().data(modifySize).message("员工身份信息成功更新 {} 条").build();
    }

    /**
     * 查询一条员工身份信息
     * @param userIdentityId 员工身份信息ID
     */
    @Override
    public ResultInfo<UserIdentityVO> queryUserIdentityById(Long userIdentityId) {
        UserIdentity userIdentity = userIdentityRepository.getUserIdentityById(userIdentityId);
        UserIdentityVO userIdentityVO = userIdentityConvert.entityToVO(userIdentity);
        return ResultInfo.<UserIdentityVO>builder().data(userIdentityVO).message("员工身份信息查询成功").build();
    }

    /**
     * 分页查询员工身份信息
     * @param userIdentityQueryDTO 员工身份信息参数
     */
    @Override
    public ResultInfo<Page<UserIdentityVO>> queryUserIdentityPage(UserIdentityDTO.QueryDTO userIdentityQueryDTO) {
        Page<UserIdentity> userIdentityPage = userIdentityRepository.getUserIdentityPage(userIdentityQueryDTO);
        Page<UserIdentityVO> resultData = userIdentityConvert.convertPage(userIdentityPage);
        return ResultInfo.<Page<UserIdentityVO>>builder()
                .data(resultData)
                .message("员工身份信息分页查询成功，共 {} 条", resultData.getRecords().size())
                .build();
    }

    /**
     * 根据条件查询 员工身份信息 数量
     * @param userIdentityQueryDTO 员工身份信息参数
     */
    @Override
    public ResultInfo<Long> queryUserIdentityCount(UserIdentityDTO.QueryDTO userIdentityQueryDTO) {
        Long resultCount = userIdentityRepository.getUserIdentityCount(userIdentityQueryDTO);
        return ResultInfo.<Long>builder()
                .data(resultCount)
                .message("员工身份信息数量查询成功，共 {} 条", resultCount)
                .build();
    }

    /**
     * 根据条件查询 员工身份信息
     * @param userIdentityQueryDTO 员工身份信息参数
     */
    @Override
    public ResultInfo<List<UserIdentityVO>> queryUserIdentityList(UserIdentityDTO.QueryDTO userIdentityQueryDTO) {
        List<UserIdentity> userIdentityList = userIdentityRepository.getUserIdentityList(userIdentityQueryDTO);
        List<UserIdentityVO> resultData = userIdentityConvert.entityListToVoList(userIdentityList);
        return ResultInfo.<List<UserIdentityVO>>builder()
                .data(resultData)
                .message("员工身份信息分页查询成功，共 {} 条", resultData.size())
                .build();
    }
}
